package com.service.frontdesk;

import com.bean.frontdesk.bo.*;
import com.bean.frontdesk.vo.OrdersVO;
import com.bean.frontdesk.vo.UpdateUserVO;
import com.bean.pojo.GoodsType;
import com.bean.pojo.User;
import com.dao.admin.AdminMapper;
import com.dao.frontdest.GoodsMapper;
import com.dao.frontdest.UserMapper;
import com.utils.MybatisUtils;
import com.utils.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author: 晓帆
 * @description:
 * @create: 2021-06-21 15:01
 **/
public class UserServiceImplement implements UserService{
    @Override
    public User login(LoginInBO login) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.selectUserByLoginInBO(login);
        return user;
    }

    @Override
    public int signUp(SignUpBO signUpBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<String> nicknames = mapper.selectNickName();
        if (nicknames.contains(signUpBO.getNickname())){
            return 404;
        }

        int insertState = mapper.insertBySignUpBO(signUpBO);
        sqlSession.close();
        if (insertState==1){
            return 200;
        }
        return 404;
    }

    @Override
    public List<GoodsType> showAllType() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        List<GoodsType> goodsTypes = mapper.selectAllTypes();
        sqlSession.close();
        return goodsTypes;
    }

    @Override
    public UpdateUserVO showUserUpdate(String token) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.selectUserByNickname(token);
        UpdateUserVO updateUserVO = new UpdateUserVO(user.getId(), user.getUsername(), user.getNickname(), user.getRecipient(), user.getAddress(), user.getTel());
        return updateUserVO;
    }

    @Override
    public int updatePwd(UpdatePwdBO updatePwdBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        String password = mapper.selectPwdById(updatePwdBO.getId());
        if (!password.equals(updatePwdBO.getOldPwd())|| StringUtils.idEmpty(updatePwdBO.getOldPwd())){
            return 303;
        }
        int updateState = mapper.updatePwdByUpdatePwdBO(updatePwdBO);
        sqlSession.close();
        if (updateState!=0){
            return 200;
        }

        return 404;
    }

    @Override
    public int updateUser(UpdateUserBO updateUserBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<String> nickName = mapper.selectNickName();
        String nickname = mapper.selectNickNameById(updateUserBO.getId());
        if (StringUtils.idEmpty(updateUserBO.getNickname())){
            return 101;
        }
        if (nickName.contains(updateUserBO.getNickname())&&!nickname.equals(updateUserBO.getNickname())){
            return 303;
        }
        int updateState = mapper.updateUserByUpdateUserBO(updateUserBO);
        if (updateState!=0){
            return 200;
        }

        return 404;
    }

    @Override
    public List<OrdersVO> showMyOrders(Integer state, String nickname) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int id = mapper.selectIdByNickName(nickname);
        List<OrdersVO> ordersVOS = mapper.selectMyOrders(state, id);

        return ordersVOS;
    }

    @Override
    public int placeOrder(OrderBO orderBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<OrderBO.oredrs> oredrs = orderBO.getOredrs();
        int updateState=0;
        for (OrderBO.oredrs oredr : oredrs) {
            int placeOrder = mapper.placeOrder(oredr);
            updateState+=placeOrder;
        }
        sqlSession.close();

        if (updateState!=0){
            return 200;
        }
        return 404;
    }

    @Override
    public int deleteOrders(int id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int i = mapper.selectOrderState(id);
        if (i==0){
            int num = mapper.selectOrderNum(id);
            int speceId = mapper.selectSpeceId(id);
            int spaceNum = mapper.selectSpaceNum(speceId);
            int newNum =spaceNum+num;
            mapper.updateSpaceNum(speceId,newNum);
        }
        int updateState = mapper.deleteOrdersByUpdateState(id);
        if (updateState!=0){
            return 200;
        }
        return 404;
    }

    @Override
    public int pay(int id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int payState = mapper.payByUpdate(id);
        if (payState!=0){
            return 200;
        }
        return 404;
    }

    @Override
    public int receiveGood(int id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int receiveState = mapper.receiveByUpdate(id);
        if (receiveState!=0){
            return 200;
        }
        return 404;
    }

    @Override
    public int evaluation(GoodEvaluationBO goodEvaluationBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        long time = System.currentTimeMillis();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sf.format(new Date(time));
        goodEvaluationBO.setCreateTime(date);
        int insertState = mapper.evaluationByObj(goodEvaluationBO);
        int i = mapper.updateOrderHashComment(goodEvaluationBO.getOrderId());
        if (insertState!=0&&i!=0){
            return 200;
        }

        return 404;
    }

    @Override
    public int addOrderByAddOrders(AddOrderBO addOrderBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int id = mapper.selectIdByNickName(addOrderBO.getToken());
        addOrderBO.setUserId(id);
        int goodId = mapper.selectGoodIdBySpecId(addOrderBO.getGoodsDetailId());
        addOrderBO.setGoodsId(goodId);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long l = System.currentTimeMillis();
        Date date = new Date(l);
        String time = sf.format(date);
        addOrderBO.setAddTime(time);
        int i = mapper.insertByAddOrder(addOrderBO);
        if (addOrderBO.getState()>0){
            int spaceNum = mapper.selectSpaceNum(addOrderBO.getGoodsDetailId());
            int num = spaceNum-addOrderBO.getNum();
            int spaceNum1 = mapper.updateSpaceNum(addOrderBO.getGoodsDetailId(), num);
        }
        if (i!=0){
            return 200;
        }

        return 404;
    }
}
